<template>
  <div class="login">
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-width="100px"
    >
      <el-form-item
        label="用户名"
        prop="username"
      >
        <el-input
          v-model="form.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="密码"
        prop="password"
      >
        <el-input
          type="password"
          v-model="form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>

      <!-- 登录 && 注册 -->
      <el-form-item>
        <el-button
          @click="onLogin"
          type="primary"
          >登录</el-button
        >
        <el-button
          @click="onRegister"
          type="primary"
          >注册</el-button
        >
        <el-button
          @click="onPassword"
          type="text"
          >忘记密码</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import bcryptjs from 'bcryptjs' // 密码加密

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      form: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    async onLogin() {
      const valid = await this.$refs.form.validate().catch(err => console.error(err))
      if (!valid) return

      //发送登录请求1. 对密码加密 2. 发送请求
      const { username, password } = this.form
      const hashPassword = await window.btoa(password)
      // const hashPassword = await bcrypt.genSaltSync(password)
      const {
        code,
        data: { token },
      } = await this.$axios
        .post('/api/login', {
          username,
          password: hashPassword,
        })
        .catch(_ => _)

      if (code === 0) {
        //登录成功
        this.$message.success('登录成功')
        this.$router.replace({
          path: '/',
          query: {
            username,
          },
        })
        window.localStorage.setItem('token', token)
        window.localStorage.setItem('username', username)
      }
    },

    async onRegister() {
      const valid = await this.$refs.form.validate().catch(err => console.error(err))
      if (!valid) return

      //发送登录请求1. 对密码加密 2. 发送请求
      const { username, password } = this.form
      const hashPassword = await window.btoa(password)
      console.log('注册时的密码-----', hashPassword)
      const { code, message } = await this.$axios
        .post('/api/register', {
          username,
          password: hashPassword,
        })
        .catch(_ => _)

      if (code === 0) {
        //注册成功
        this.$message.success('注册成功')
      } /*  else {
        //登录失败
        this.$message.error(message || '用户名已存在')
      } */
    },

    onPassword() {},
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
}
</script>
<style
  lang="scss"
  scoped
>
.login {
  width: 100%;
  height: 100%;
  line-height: 100vh;
  text-align: center;
  background-image: url(../assets/login.gif);
  background-size: 100% 100%;
}
.el-form {
  width: 400px;
  display: inline-block;
  vertical-align: middle;
}
</style>
